package com.zbkj.crmeb.merchant.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.constants.Constants;
import com.zbkj.crmeb.member.model.ChannelMerchantAttribute;
import com.zbkj.crmeb.member.service.IChannelMerchantAttributeService;
import com.zbkj.crmeb.merchant.admin.service.CustomShareService;
import com.zbkj.crmeb.merchant.api.response.MerchantOrderTimeResponse;
import com.zbkj.crmeb.store.dao.CustomShareDao;
import com.zbkj.crmeb.store.model.CustomShare;
import com.zbkj.crmeb.system.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
* @author Mr.Zhang
* @Description IndexServiceImpl 接口实现
* @since 2020-04-13
*/
@Service
public class CustomShareServiceImpl extends ServiceImpl<CustomShareDao, CustomShare> implements CustomShareService {

    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private IChannelMerchantAttributeService channelMerchantAttributeService;

    @Override
    public MerchantOrderTimeResponse getOrderTime(Integer merId) {
        MerchantOrderTimeResponse response = new MerchantOrderTimeResponse();
        String key = Constants.CONFIG_KEY_ORDER_CANCEL_TIME + "_" + merId;
        response.setOrderTime(StringUtils.isEmpty(systemConfigService.getValueByKey(key)) ? 0 : Integer.parseInt(systemConfigService.getValueByKey(key)));
        ChannelMerchantAttribute channelMerchantAttribute = channelMerchantAttributeService.getOne(Wrappers.lambdaQuery(ChannelMerchantAttribute.class).eq(ChannelMerchantAttribute::getMerId,merId));
        if (Objects.nonNull(channelMerchantAttribute)){
            response.setActivityOrderTime(channelMerchantAttribute.getActivityOrderTime());
        }
        return response;
    }

    @Override
    public Boolean updOrderTime(Integer orderTime, Integer activityOrderTime, Integer merId) {
        String key = Constants.CONFIG_KEY_ORDER_CANCEL_TIME + "_" + merId;
        systemConfigService.updateOrSaveValueByName(key, orderTime.toString());
        ChannelMerchantAttribute channelMerchantAttribute = channelMerchantAttributeService.getOne(Wrappers.lambdaQuery(ChannelMerchantAttribute.class).eq(ChannelMerchantAttribute::getMerId,merId).select(ChannelMerchantAttribute::getId));
        if (Objects.nonNull(channelMerchantAttribute)){
            channelMerchantAttribute.setActivityOrderTime(activityOrderTime);
            channelMerchantAttributeService.updateById(channelMerchantAttribute);

        }
        return true;
    }
}